package com.zut.chat.controller;

import com.zut.chat.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 视频测试控制器
 * 用于测试视频服务功能
 */
@Slf4j
@RestController
@RequestMapping("/api/video")
@CrossOrigin(origins = "*")
public class VideoTestController {

    @Autowired
    private VideoService videoService;

    /**
     * 获取随机视频URL（用于测试）
     */
    @GetMapping("/random")
    public ResponseEntity<Map<String, Object>> getRandomVideo() {
        try {
            return videoService.getRandomVideoUrl()
                    .map(videoUrl -> {
                        Map<String, Object> response = new HashMap<>();
                        if (videoUrl != null && !videoUrl.trim().isEmpty()) {
                            response.put("success", true);
                            response.put("videoUrl", videoUrl);
                            response.put("message", "获取视频URL成功");
                        } else {
                            response.put("success", false);
                            response.put("message", "获取视频URL失败");
                        }
                        return ResponseEntity.ok(response);
                    })
                    .block(); // 注意：在实际生产环境中应该使用异步方式
        } catch (Exception e) {
            log.error("获取随机视频失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取视频失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 验证视频URL是否有效
     */
    @PostMapping("/validate")
    public ResponseEntity<Map<String, Object>> validateVideo(@RequestParam String videoUrl) {
        try {
            return videoService.validateVideoUrl(videoUrl)
                    .map(isValid -> {
                        Map<String, Object> response = new HashMap<>();
                        response.put("success", true);
                        response.put("valid", isValid);
                        response.put("message", isValid ? "视频URL有效" : "视频URL无效");
                        return ResponseEntity.ok(response);
                    })
                    .block();
        } catch (Exception e) {
            log.error("验证视频URL失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "验证失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 